package org.demo6;

import org.apache.log4j.Logger;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.ItemReadListener;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;

public class StepStopListener<T> implements StepExecutionListener, ItemReadListener<T> {
    private StepExecution stepExecution;
    private Boolean stop = Boolean.FALSE;

    private Logger logger = Logger.getLogger(this.getClass());

    public void setStop(Boolean stop) {
        this.stop = stop;
    }

    @Override
    public void beforeStep(StepExecution stepExecution) {
        this.stepExecution = stepExecution;
    }

    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        return null;
    }

    @Override
    public void beforeRead() {
        if (stop) {
            logger.info("收到通知了！");
            stepExecution.setTerminateOnly();
        }
    }

    @Override
    public void afterRead(T item) {

    }

    @Override
    public void onReadError(Exception ex) {

    }
}
